#!/sbin/openrc-run
supervisor=supervise-daemon

description="logs, events, or other data transporter"

: ${LS_USER:="logstash"}
: ${LS_GROUP:="$(id -gn $LS_USER)"}

nice="19"
pidfile="/run/$RC_SVCNAME.sd.pid"
supervise_daemon_args="-u $LS_USER -g $LS_GROUP -p $pidfile -N $nice"
command=/usr/share/logstash/bin/logstash
command_args="--path.settings ${LS_CONF} ${LS_OPTS}"
max_fd="16384"

depends() {
	use net
}

in_contr() {
	grep "container=" /proc/1/environ
}

start_pre() {
	local dir

	# Note: checkpath doesn't create intermediate directories.
	for dir in "${LS_HOME}" "${LS_DATA}" "${LS_LOGS}"; do
		mkdir -p "$(dirname "$dir")"
	done

	checkpath -d -o $LS_USER:$LS_GROUP -m755 "${LS_HOME}"
	checkpath -d -o $LS_USER:$LS_GROUP -m700 "${LS_DATA}"
	checkpath -d -o $LS_USER:$LS_GROUP -m755 "${LS_LOGS}"

	if [ -n "$max_fd" ]; then
		if [ -z "$(in_contr)" ]; then
			ulimit -n "$max_fd" && einfo "Max open filedescriptors: $max_fd"
		else
			einfo "Run 'ulimit -n $max_fd' on the container host"
		fi
	fi
}
